package com.fenghongzhang.day003;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private long time = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            Toast.makeText(this, "再按就推出", Toast.LENGTH_SHORT).show();
            if(System.currentTimeMillis() - time > 2000){
                time = System.currentTimeMillis();
                //一个事件,是否向下继续传递.
                return true;
            }else{
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    public void send(View view) {

        //通知管理器  负责通知的取消和发送
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


        /**
         *
         *         User.Builder builder = new User.Builder();
         *         builder.setName("小明").setAddress("北京");
         *         User user = builder.build();
         *
         *
         */

        Notification.Builder builder = new Notification.Builder(this);

        //小图标和内容
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("标题")
                .setContentText("内容");

        //时间
        builder.setWhen(System.currentTimeMillis());

        //读完了自己删除
        builder.setAutoCancel(true);

        Notification build = builder.build();

        //发送
        manager.notify(1,build);

    }

    public void send1(View view) {


        //通知管理器  负责通知的取消和发送
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Notification.Builder builder0 = new Notification.Builder(this);
        Notification.Builder builder1 = new Notification.Builder(this);
        Notification.Builder builder2 = new Notification.Builder(this);
        //分组
        builder0.setGroup("feng");
        builder0.setSmallIcon(R.mipmap.ic_launcher);
        builder0.setContentTitle("build0");

        builder1.setGroup("feng");
        //舍弃自己的内容,让同组的显示.
        builder1.setGroupSummary(true);
        builder1.setSmallIcon(R.mipmap.ic_launcher);
        builder1.setContentTitle("build1");

        builder2.setGroup("feng");
        builder2.setSmallIcon(R.mipmap.ic_launcher);
        builder2.setContentTitle("build2");

        Notification build0 = builder0.build();
        Notification build1 = builder1.build();
        Notification build2 = builder2.build();
        //发送
        //相同id代表更新. 不同id才是新通知.
        manager.notify(1,build0);
        manager.notify(2,build1);
        manager.notify(3,build2);
    }

    public void send2(View view) {
        //通知管理器  负责通知的取消和发送
        final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        final Notification.Builder builder0 = new Notification.Builder(this);

        builder0.setSmallIcon(R.mipmap.ic_launcher);
        builder0.setContentTitle("进度条");

        //进度设置
        builder0.setProgress(0,0,true);

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int index =0 ;
            @Override
            public void run() {
                builder0.setProgress(100,index,false);
                index += 10;

                //要不停的去更新通知
                manager.notify(1,builder0.build());
                //如果index ==100
                if(index == 100){
                    timer.cancel();
                    //取消掉通知
                    manager.cancel(1);
                }

            }
        },0,1000);

        //相同id代表更新. 不同id才是新通知.
        manager.notify(1,builder0.build());
    }

    public void send3(View view) {
        //通知管理器  负责通知的取消和发送
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Notification.Builder builder0 = new Notification.Builder(this);

        builder0.setSmallIcon(R.mipmap.ic_launcher);
        builder0.setContentTitle("锁屏通知");

        //锁屏通知
        builder0.setVisibility(Notification.VISIBILITY_PUBLIC);
        //两个属性让通知谈出来
        /**
         * DEFAULT_ALL
         * 灯光
         * 声音
         * 震动
         */
        builder0.setDefaults(Notification.DEFAULT_ALL);
        //优先级  最大才能弹出来.
        builder0.setPriority(Notification.PRIORITY_MAX);

        //页面间的跳转用谁 intent
        Intent intent = new Intent(this,Main2Activity.class);
        //延迟的intent
        //去那个组件,就get那个组件
        PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);

        //和通知结合起来了.
        builder0.setContentIntent(activity);

        //相同id代表更新. 不同id才是新通知.
        manager.notify(1,builder0.build());


    }

    public void send5(View view) {

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder0 = new Notification.Builder(this);
        builder0.setSmallIcon(R.mipmap.ic_launcher);
        builder0.setContentTitle("锁屏通知");

        //如果是远程服务异常. 查看布局是否支持.
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_layout);
        //自定义内容的通知
        builder0.setCustomContentView(remoteViews); //24
        //22
//      builder0.setContent(remoteViews);//

        //相同id代表更新. 不同id才是新通知.
        manager.notify(1,builder0.build());



    }

    public void send6(View view) {


        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder0 = new Notification.Builder(this);
        builder0.setSmallIcon(R.mipmap.ic_launcher);
        builder0.setContentTitle("锁屏通知");

        //如果是远程服务异常. 查看布局是否支持.
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_layout);

        remoteViews.setTextViewText(R.id.notify_name,"大明");
        remoteViews.setImageViewResource(R.id.notify_img,R.mipmap.ic_launcher);

        //自定义内容的通知
        builder0.setCustomContentView(remoteViews); //24
        //22
//      builder0.setContent(remoteViews);//

        //相同id代表更新. 不同id才是新通知.
        manager.notify(2,builder0.build());
    }

    public void send7(View view) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder0 = new Notification.Builder(this);
        builder0.setSmallIcon(R.mipmap.ic_launcher);
        builder0.setContentTitle("通知样式");

        //通知内容为大图片
        Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
        bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));

        builder0.setStyle(bigPictureStyle);


        //弹出
        builder0.setPriority(Notification.PRIORITY_MAX);
        builder0.setDefaults(Notification.DEFAULT_ALL);

        //跳转
        Intent intent = new Intent(this, Main2Activity.class);
        PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
        builder0.setContentIntent(activity);

        //设置读取完毕取消
        builder0.setAutoCancel(true);

        //相同id代表更新. 不同id才是新通知.
        manager.notify(2,builder0.build());


    }
}
